package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.service.RoleResourcesService;
import com.zany.system.vo.RoleResourcesVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.BeansUtil.RequestBeanUtil;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JsonTools;


/**
 * 角色管理
 * @author chengY
 * @version 1.0
 */
public class RoleInfoAction extends AbstractActionSupport{

	private static final long serialVersionUID = -4961253401698398466L;
	private static Log logger = LogFactory.getLog(RoleInfoAction.class);
	private RoleResourcesService roleResourcesService;
	private RoleResourcesVO roleResourcesVO = new RoleResourcesVO();
	private List<RoleResourcesVO> roleList = new ArrayList<RoleResourcesVO>();

	public void getList() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("RoleInfoAction.getList()");}
		response = ServletActionContext.getResponse();
		roleResourcesVO.setRoleCode(request.getParameter("roleCode"));
		Integer pageTotal = roleResourcesService.getRoleCount();
		String start = request.getParameter("start");
		String pageStart = (start== null || start.equals("")?"1":start);
		roleResourcesVO.setPageMessage(Integer.valueOf(pageStart));
		roleList = roleResourcesService.getRoleListPage(roleResourcesVO);
		AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(roleList) + "}", response);
	}

	public void insert() throws Exception{
		if(logger.isInfoEnabled()){logger.info("RoleInfoAction.insert()");}
		response = ServletActionContext.getResponse();
		roleResourcesVO = (RoleResourcesVO)RequestBeanUtil.getReqToBean(RoleResourcesVO.class,request);
		try{
			RoleResourcesVO vo = new RoleResourcesVO();
			vo = roleResourcesService.getRoleInfo(roleResourcesVO);
			if(vo!=null){
				AjaxTools.exAjax("{\"success\":false,msg:\"该角色代码已存在！\"}",response);
				return;
			}
			roleResourcesService.insertRole(roleResourcesVO);
			AjaxTools.exAjax("{success:true}",response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	
	public void delete() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("RoleInfoAction.delete()");}
		response = ServletActionContext.getResponse();
		String delId[] = request.getParameterValues("delId");
		try{
			if(delId.length>0){
				for (int i = 0; i < delId.length; i++){
					roleResourcesVO.setRoleCode(delId[i]);
					roleResourcesService.deleteRole(roleResourcesVO);
				}
				AjaxTools.exAjax("{success:true}", response);
			}
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
		
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
	}
	public RoleResourcesService getRoleResourcesService() {
		return roleResourcesService;
	}
	public void setRoleResourcesService(RoleResourcesService roleResourcesService) {
		this.roleResourcesService = roleResourcesService;
	}
}
